<?php

class DanhsachnhathauController extends BackEndController {

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex() {
        $model = new DanhSachNhaThauForm();
        $model->unsetAttributes(); // clear any default values
        $model->type = FormData::DANH_SACH_NHA_THAU_THAM_GIA_DAU_THAU;
        if (isset($_GET['DanhSachNhaThauForm'])) {
            $model->attributes = $_GET['DanhSachNhaThauForm'];
        }
        $this->render('index', array(
            'model' => $model,
        ));
        
    }

    public function actionCreate() {
        $model = new DanhSachNhaThauForm();
        if (isset($_POST['DanhSachNhaThauForm'])) {
            $model->attributes = $_POST['DanhSachNhaThauForm'];
            $model->user_create = Yii::app()->user->id;
            $model->user_update = Yii::app()->user->id;
            $model->created_at = date('Y-m-d H:i:s');
            $model->updated_at = date('Y-m-d H:i:s');
            if ($model->validate() && $model->save()) {
                Yii::app()->user->setFlash('success', 'Tạo mới thành công.');
                $this->redirect(array('danhsachnhathau/update', 'id' => $model->id));
            } else {
                Yii::app()->user->setFlash('error', 'Đã có lỗi xảy ra. Vui lòng kiểm tra lại.');
            }
        }
        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = new DanhSachNhaThauForm();
        $model->loadById($id);
        if (isset($_POST['DanhSachNhaThauForm'])) {
            $model->attributes = $_POST['DanhSachNhaThauForm'];
            $model->user_update = Yii::app()->user->id;
            $model->updated_at = date('Y-m-d H:i:s');
            if ($model->validate() && $model->save()) {
                Yii::app()->user->setFlash('success', 'Cập nhật thành công.');
                $this->refresh();
            } else {
                Yii::app()->user->setFlash('error', 'Đã có lỗi xảy ra. Vui lòng kiểm tra lại.');
            }
        }
        $this->render('update', array('model' => $model));
    }

}
